package com.efast.dmom.mgmt.openws;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.cafeauto.ws.Message;
import com.efast.cafe.commponent.es.webservice.annotation.CafeWSService;
import com.efast.cafe.commponent.es.webservice.base.WSAbstractAdapter;
import com.efast.cafe.commponent.es.webservice.utils.W3CElementUtil;
import com.efast.cafe.framework.spring.SpringContextUtils;
import com.efast.dmom.mgmt.bean.mes.testline.LightData;
import com.efast.dmom.mgmt.bean.mes.testline.LightDataBean;
import com.efast.dmom.mgmt.bean.mes.testline.LightDataBizContent;
import com.efast.dmom.mgmt.bean.mes.testline.LightInboundDataSet;
import com.efast.dmom.mgmt.bean.mes.testline.TestLineDataLogBean;
import com.efast.dmom.mgmt.service.mes.testline.impl.TestLineService;

@CafeWSService(bizCode = "Light_Test")
public class LightDataService extends WSAbstractAdapter {

	@Override
	public Message handleMessage(Message message) {
		
		logger.info("Received Light data from: " + message.getBIZ_CODE());

		LightDataBizContent content = W3CElementUtil.elementUnmarshalToObj(message.getBIZ_CONTENT(),
				LightDataBizContent.class);
		LightInboundDataSet inboundData = content.getLightInboundDataSet();
		List<LightData> dataList = inboundData.getLightData();

		if(dataList != null && dataList.size() >0){
			
			List<LightDataBean> list = new ArrayList<LightDataBean>();
			for (LightData data : dataList) {

				LightDataBean dataBean = new LightDataBean();

				dataBean.setVin(data.getVin());
				dataBean.setLeftLightFar(data.getLeftLightFar());
				dataBean.setLeftLightFarLevel(data.getLeftLightFarLevel());
				dataBean.setLeftLightFarVertical(data.getLeftLightFarVertical());
				dataBean.setLeftLightIntensity(data.getLeftLightIntensity());
				dataBean.setLeftLightNear(data.getLeftLightNear());
				dataBean.setLeftLightNearLevel(data.getLeftLightNearLevel());
				dataBean.setLeftLightNearVertical(data.getLeftLightNearVertical());
				dataBean.setRightLightFar(data.getRightLightFar());
				dataBean.setRightLightFarLevel(data.getRightLightFarLevel());
				dataBean.setRightLightFarVertical(data.getRightLightFarVertical());
				dataBean.setRightLightIntensity(data.getRightLightIntensity());
				dataBean.setRightLightNear(data.getRightLightNear());
				dataBean.setRightLightNearLevel(data.getRightLightNearLevel());
				dataBean.setRightLightNearVertical(data.getRightLightNearVertical());
				dataBean.setLightResult(data.getLightResult());
				dataBean.setTime(data.getTime());
				dataBean.setTestNumber(data.getTestNumber());
				
				dataBean.setCreateTime(new Date());
				dataBean.setCreateUser(message.getBIZ_CODE());

				list.add(dataBean);
			}

			TestLineService service = SpringContextUtils.getBean(TestLineService.class);
			service.insertLightData(list);
			
			TestLineDataLogBean logBean = new TestLineDataLogBean();
			logBean.setVin(dataList.get(0).getVin());
			logBean.setStatus("保存");
			logBean.setReceivedTime(new Date());
			logBean.setSource(message.getMESSAGE_FROM());
			logBean.setDestination(message.getMESSAGE_TO());
			
			service.insertTestLineLog(logBean);
		}
		

		message.setBIZ_CONTENT(null);
		message.setMESSAGE_FROM("MES");
		message.setMESSAGE_TO("Ligth_Test");
		message.setRET_CODE("200");
		message.setRET_CODE_DESC("Successful");
		return message;
	}

}
